package org.example.unfiar;

import java.util.*;
import java.util.concurrent.CopyOnWriteArrayList;

public class ListTest {         // java.util.ConcurrentModificationException 并发修改异常
    // 测试List线程安全性
    public static void main(String[] args) {

        // 解决方法：
        // 1. List<String> list = new Vector<>(); JDK1.0
        // 2. List<String> list = Collections.synchronizedList(oldList); 工具类
        // 3. List<String> list = new CopyOnWriteArrayList<>(); JUC 调用 Lock

        List<String> list = new CopyOnWriteArrayList<>();

        for (int i = 1; i <= 15; i++) {
            new Thread(() -> {
                list.add(UUID.randomUUID().toString().substring(0,5));
                System.out.println(list);
            }, String.valueOf(i)).start();
        }

        // list.forEach(System.out::println);
    }
}
